﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ClassLibraryForMusic;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        static public Album Альбом = new Album();

        public Form1()
        {
            InitializeComponent();
        }

        private void Сделать_пустыми()
        {
            labelAlterType.Text = "-";
            labelAuthor.Text = "-";
            labelLength.Text = "-";
            labelrockers.Text = "-";
            labelInstruments.Text = "-";
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1) labelGeneralLength.Text = "0";
            else
            {
                int genLength = 0;
                for (int i = 0; i < listBox1.SelectedIndices.Count; i++)
                    genLength += Альбом.GetMusicFileByIndex(listBox1.SelectedIndices[i]).Length;
                labelGeneralLength.Text = genLength.ToString();
            }

            if (listBox1.SelectedIndices.Count == 1)
            {
                if (listBox1.SelectedIndex != -1)
                {
                    Сделать_пустыми();
                    Album.AbstractMusicFile musicFile = Альбом.GetMusicFileByIndex(listBox1.SelectedIndex);
                    if (musicFile != null)
                    {
                        labelLength.Text = musicFile.Length.ToString();
                        labelAuthor.Text = musicFile.Author;
                    }
                    if (typeof(Album.ClassicMusicFile).IsInstanceOfType(musicFile))
                    {
                        Album.ClassicMusicFile classic = (Album.ClassicMusicFile)musicFile;
                        string orchestraInstruments = "";
                        if (classic.OrchestraInstruments.Count >= 1)
                        {
                            orchestraInstruments = classic.OrchestraInstruments[0];
                            for (int i = 1; i < classic.OrchestraInstruments.Count; i++)
                                orchestraInstruments += ", " + classic.OrchestraInstruments[i];
                        }
                        labelInstruments.Text = orchestraInstruments;
                    }
                    if (typeof(Album.RockMusicFile).IsInstanceOfType(musicFile))
                    {
                        Album.RockMusicFile rock = (Album.RockMusicFile)musicFile;
                        string rockers = "";
                        if (rock.Rockers.Count >= 1)
                        {
                            rockers = rock.Rockers[0];
                            for (int i = 1; i < rock.Rockers.Count; i++)
                                rockers += ", " + rock.Rockers[i];
                        }
                        labelrockers.Text = rockers;
                        string rockinstruments = "";
                        if (rock.RockInstruments.Count >= 1)
                        {
                            rockinstruments = rock.RockInstruments[0];
                            for (int i = 1; i < rock.RockInstruments.Count; i++)
                                rockinstruments += ", " + rock.RockInstruments[i];
                        }
                        labelInstruments.Text = rockinstruments;
                        if (typeof(Album.AlternativeRockMusicFile).IsInstanceOfType(rock))
                        {
                            Album.AlternativeRockMusicFile al = (Album.AlternativeRockMusicFile)rock;
                            labelAlterType.Text = al.TypeOfAlternativeRock.ToString();
                        }
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            new Form2().ShowDialog();
            Обновить_список_композиций();
        }

        private void Обновить_список_композиций()
        {
            listBox1.Items.Clear();
            int I = 0;
            Album.AbstractMusicFile musicFile;
            do
            {
                musicFile = Альбом.GetMusicFileByIndex(I);
                if (musicFile != null)
                    listBox1.Items.Add(musicFile.Name_of_File);
                ++I;
            } while (musicFile != null);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string filename = dialog.FileName;
                List<int> indices = new List<int>();
                for (int i = 0; i < listBox1.SelectedIndices.Count; i++)
                    indices.Add(listBox1.SelectedIndices[i]);
                Альбом.WriteOnDisk(filename, indices);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string filename = dialog.FileName;
                Альбом.LoadFromDisk(filename);
				Обновить_список_композиций();
            }
        }
    }
}
